import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.*;
import java.security.spec.ECGenParameterSpec;

public class sm2_demo {
    private static String M = "XY_SM2_demo";

    public static void main(String[] args) throws NoSuchAlgorithmException,
            InvalidAlgorithmParameterException {
        SM2Util sm2 = new SM2Util();
        final ECGenParameterSpec sm2Spec = new ECGenParameterSpec("sm2p256v1");
// 获取一个椭圆曲线类型的密钥对生成器
        final KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", new BouncyCastleProvider());
// 使用SM2参数初始化生成器
        kpg.initialize(sm2Spec);
// 获取密钥对
        KeyPair keyPair = kpg.generateKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();
        System.out.println("明文：" + M);
        String data = sm2.encrypt(publicKey, M);
        System.out.println("加密结果：" + data);
        String text = sm2.decrypt(privateKey, data);
        System.out.println("解密结果：" + text);
    }
}